package dk.dtu.android.transport.db;

import java.util.ArrayList;

import dk.dtu.android.rejseplanen.StopLocation;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;

public class StopstationTable extends BaseDbAdapter {

	protected static String TABLE_NAME = "STOPSTATION";
	
	protected static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY, stopstation_id INTEGER, name TEXT, x TEXT, y TEXT, freq INTEGER, zone INTEGER)";
	
	public StopstationTable(String key, String value) {
	}
	
	/* Static methods */
	
	public static void insert(String stopstationId, String name, String x, String y)
	{
		ContentValues args = new ContentValues();
		args.put("stopstation_id", stopstationId);
		args.put("name", name);
		args.put("x", x);
		args.put("y", y);
		args.put("freq", 1);
		
		db.insert(TABLE_NAME, null, args);
	}
	
	public static void insertIfNotExists(String stopstationId, String name, String x, String y)
	{
		Cursor cursor = db.query(TABLE_NAME, new String[] {"_id"}, "stopstation_id = ?", new String[] {stopstationId}, null, null, null);
		
		if (!cursor.moveToFirst())
		{
			insert(stopstationId, name, x, y);
		}
		else
		{
			incFreq(stopstationId);
		}
	}
	
	public static void updateZone(int id, int zone)
	{
		ContentValues args = new ContentValues();
		args.put("zone", zone);
		db.update(TABLE_NAME, args, "_id = ?", new String[] {Integer.toString(id)});
	}
	
	public static void incFreq(String stopstationId)
	{
		Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "freq", }, "stopstation_id = ?", new String[] {stopstationId}, null, null, null);
		
		Integer freq = null;
		if (cursor.moveToFirst())
		{
			do {
				freq = cursor.getInt(1);
			} while (cursor.moveToNext());
		}
		
		if (freq != null)
		{
			ContentValues args = new ContentValues();
			args.put("freq", freq+1);
			db.update(TABLE_NAME, args, "stopstation_id = ?", new String[] {stopstationId});
		}
	}
	
	public static StopLocation getMostFrequent(ArrayList<String> ids)
	{
		if (ids.size() == 0)
			return null;
		String[] select = new String[] {"_id", "stopstation_id", "name", "x", "y", "freq"};
		String ids_where = TextUtils.join(", ", ids);
		String where = "stopstation_id in (" + ids_where + ")";
		Cursor cursor = db.query(TABLE_NAME, select, where, null, null, null, "freq DESC");
		if (cursor.moveToFirst())
		{
			StopLocation loc = new StopLocation();
			loc.id = cursor.getString(1);
			loc.name = cursor.getString(2);
			loc.x = cursor.getString(3);
			loc.y = cursor.getString(4);
			return loc;
		}
		else
		{
		  return null;	
		}
	}
	
	public static void clearTable()
	{
		db.delete(TABLE_NAME, null, null);
	}
	
	public static ArrayList<String> getStopstations()
	{
		String[] select = new String[] {"_id", "name"};
		Cursor cursor = db.query(TABLE_NAME, select, null, null, null, null, "freq DESC");
		
		ArrayList<String> stopstations = new ArrayList<String>();
		
		if (cursor.moveToFirst())
		{
			do {
				stopstations.add(cursor.getString(1));
			} while (cursor.moveToNext());
		}
		return stopstations;
	}
}
